﻿<%@ Page Language="C#" %>

<script runat="server">
    protected void Page_Load(object sender, EventArgs e)
    {
        Response.Buffer = true;
        Response.ExpiresAbsolute = System.DateTime.Now.AddSeconds(-1);
        Response.Expires = 0; Response.CacheControl = "no-cache";

        HttpPostedFile file = Request.Files[0];
        string uploadpath = Server.MapPath(Request["folder"] + "\\" + DateTime.Now.ToShortDateString() + "\\");
        if (file != null)
        {
            if (!System.IO.Directory.Exists(uploadpath))
            {
                System.IO.Directory.CreateDirectory(uploadpath);
            }
            string strNewFileName = GetNewFileName(file.FileName);
            file.SaveAs(uploadpath + strNewFileName);
            Response.Write(Request["folder"] + '/' + DateTime.Now.ToShortDateString() + "/" + strNewFileName);
        }
        else
        {
            Response.Write("0");
        }
    }
    /// <summary>
    /// 根据原文件名 生成 随机文件名
    /// </summary>
    /// <param name="FileName">原文件名</param>
    /// <returns>新的文件名</returns>
    private string GetNewFileName(string FileName)
    {//跟据文伯名产生一个由时间+随机数组成的一个新的文件名 
        string newfilename = DateTime.Now.Year.ToString() + DateTime.Now.Month.ToString()
        + DateTime.Now.Day.ToString() + DateTime.Now.Hour.ToString()
        + DateTime.Now.Second.ToString() + DateTime.Now.Minute.ToString()
        + DateTime.Now.Millisecond.ToString()
            //+ rand.Next(1000).ToString() 
        + FileName.Substring(FileName.LastIndexOf("."), FileName.Length - FileName.LastIndexOf("."));
        return newfilename;
    } 
</script>
